﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IeidjtuKCB.Model;
using pingjunfen.DBSession;
using Dos.ORM;
namespace pingjunfen
{
    class Program
    {
        static void Main(string[] args)
        {
            
            var student = new Student();
            if (args.Length<=0)
            {
                 student = DataBase.Context.From<Student>().Where(x => x.GradeID==11).First();
            }
            else
            {
                student = DataBase.Context.From<Student>().Where(x => x.StdCode == args.First()).First();
            }
            if (student == null)
            {
                Console.WriteLine("学号没查到");
                return;
            }
            var stureports = DataBase.Context.From<vw_StuReport>().Where(x => x.StuID == student.StdID &&  x.State.Contains("归档")).OrderBy(x=>x.ATYID).ToList();
            StringBuilder sb=new StringBuilder();
            var finalreports = new List<FinalReport>();
            float sumcj = 0;
            foreach (var stureport in stureports)
            {
                var stureport_Convered = ConvertCJ(stureport);
                float finalcj = 0;
                try
                {
                    if (float.TryParse(stureport_Convered.成绩,out finalcj))
                    {
                        if (finalcj >= 60)
                        {
                            var num = finalreports.Where(n => n.StuReport.CCID == stureport.CCID).ToList().Count;
                            var havereport = num > 0
                                ? finalreports.Where(n => n.StuReport.CCID == stureport.CCID).ToList().First()
                                : null;
                            if (havereport != null)
                            {
                                if (float.Parse(stureport_Convered.成绩) > float.Parse(havereport.StuReport.成绩))
                                {
                                    finalreports.Remove(havereport);
                                }
                            }
                            FinalReport f = new FinalReport();
                            f.StuReport = stureport_Convered;
                            f.finalcj = finalcj;
                            finalreports.Add(f);
                        }
                    }
                    else
                    {
                        Console.WriteLine("成绩格式错误srid="+stureport_Convered.SRID);
                    }
                }
                catch (FormatException e)
                {
                    Console.WriteLine("成绩格式错误srid=" + stureport_Convered.SRID);
                    throw;
                }

            }
            foreach (var finalreport in finalreports)
            {
                sb.Append("学期");
                sb.Append(finalreport.StuReport.当前学期);
                sb.Append(" 课程名");
                sb.Append(finalreport.StuReport.课程名称);
                sb.Append(" 成绩;");
                sb.Append(finalreport.finalcj);
                sb.AppendLine();
                sumcj = sumcj + finalreport.finalcj;
            }
            Console.WriteLine(student.StdCode+" "+student.StdName);
            Console.WriteLine(sb.ToString());
            Console.WriteLine("共"+finalreports.Count+"科目,总分:"+sumcj+",平均分:"+(sumcj/finalreports.Count));
        }
      
        //将成绩转为数字格式
        private static vw_StuReport ConvertCJ(vw_StuReport stureport)
        {
            if (stureport.成绩.Contains("优"))
            {
                stureport.成绩 = "-95";
            }
            else if (stureport.成绩.Contains("良"))
            {
                stureport.成绩 = "-85";
            }
            else if (stureport.成绩.Contains("中"))
            {
                stureport.成绩 = "-75";
            }
            else if ( stureport.成绩=="及格")
            {
                stureport.成绩 = "-65";
            }
            else if (stureport.成绩.Contains("不及格")|| stureport.成绩.Contains("合格") )
            {
                stureport.成绩 = "-59";
            }
            else if (stureport.成绩.ToUpper().Contains("NULL") || stureport.成绩.Contains("无") ||
                     string.IsNullOrEmpty(stureport.成绩) || stureport.成绩.ToUpper() == "NULL" || stureport.成绩.Length == 0)
            {
                stureport.成绩 = "-1";
            }
            else
            {

                try
                {
                    stureport.成绩 = ((int)float.Parse(stureport.成绩)).ToString();
                }
                catch (Exception)
                {

                   Console.WriteLine("成绩转换错误:" + stureport.成绩 + "srid:" + stureport.SRID);
                }

            }
            return stureport;
        }

      
    }

    public class FinalReport
    {
        public vw_StuReport StuReport { get; set; }
        public float finalcj { get; set; }
    }
}
